/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <fcntl.h>
#include <cstdio>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include "It_process_plimits.h"
#include <mqueue.h>

#define MQ_OPEN_COUNT_MAX 3
const int MQUEUE_STANDARD_NAME_LENGTH  = 50;

mqd_t g_mqueue_017_1[MQ_OPEN_COUNT_MAX];
CHAR g_mqname_017_1[MQUEUE_STANDARD_NAME_LENGTH] = "";
int g_index_017_1 = 0;
mqd_t g_mqueue_017_2[MQ_OPEN_COUNT_MAX];
CHAR g_mqname_017_2[MQUEUE_STANDARD_NAME_LENGTH] = "";
int g_index_017_2 = 0;

extern UINT32 LosCurTaskIDGet();

int Fork_017_1()
{
    pid_t pid;
    int ret;
    char pidStr[3] = { 0 };
    struct mq_attr attr = { 0 };
    attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
    attr.mq_maxmsg = 1;
    std::string g_configFileProcs = "/proc/plimits/test0/test1/plimits.procs";

    pid = fork();
    sprintf_s(pidStr, sizeof(pidStr), "%d", getpid());
    if(pid < 0) {
        return 0;
    } else if (pid == 0) {
        ret = WriteFile(g_configFileProcs.c_str(), pidStr);
        ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
        for (g_index_017_1 = 0; g_index_017_1 < 6; ++g_index_017_1) {
            snprintf(g_mqname_017_1, MQUEUE_STANDARD_NAME_LENGTH, "/mq_017_1_%d_%d", g_index_017_1, LosCurTaskIDGet());
            g_mqueue_017_1[g_index_017_1] = mq_open(g_mqname_017_1, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
            ICUNIT_ASSERT_NOT_EQUAL(g_mqueue_017_1[g_index_017_1], (mqd_t)-1, g_mqueue_017_1[g_index_017_1]);
        }
        sleep(30);
        exit(0);
    } else if (pid > 0) {
        return 0;
    }
    exit(0);
}

int Fork_017_2()
{
    pid_t pid;
    int ret;
    char pidStr[3] = { 0 };
    struct mq_attr attr = { 0 };
    attr.mq_msgsize = MQUEUE_STANDARD_NAME_LENGTH;
    attr.mq_maxmsg = 1;
    std::string g_configFileProcs = "/proc/plimits/test0/test1/test2/plimits.procs";

    pid = fork();
    sprintf_s(pidStr, sizeof(pidStr), "%d", getpid());
    if(pid < 0) {
        return 0;
    } else if (pid == 0) {
        ret = WriteFile(g_configFileProcs.c_str(), pidStr);
        ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
        for (g_index_017_2 = 0; g_index_017_2 < 8; ++g_index_017_2) {
            snprintf(g_mqname_017_2, MQUEUE_STANDARD_NAME_LENGTH, "/mq_017_2_%d_%d", g_index_017_2, LosCurTaskIDGet());
            g_mqueue_017_2[g_index_017_2] = mq_open(g_mqname_017_2, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
            ICUNIT_ASSERT_NOT_EQUAL(g_mqueue_017_2[g_index_017_2], (mqd_t)-1, g_mqueue_017_2[g_index_017_2]);
        }
        sleep(30);
        exit(0);
    } else if (pid > 0) {
        return 0;
    }
    exit(0);
}

static UINT32 Testcase(VOID)
{
    mode_t mode;
    int ret;
    pid_t wpid;
    int status;
    char buf[512] = { 0 };
    char *array[4] = { 0 };    
    int mqSuccessCount = -1;

    std::string subPlimitsPath_0 = "/proc/plimits/test0";
    std::string subPlimitsPath_1 = "/proc/plimits/test0/test1";
    std::string subPlimitsPath_2 = "/proc/plimits/test0/test1/test2";
    std::string g_configFileMqCount_1 = "/proc/plimits/test0/test1/ipc.mq_count";
    std::string g_configFileMqCount_2 = "/proc/plimits/test0/test1/test2/ipc.mq_count";
    std::string g_mqLimitCount_1 = "6";
    std::string g_mqLimitCount_2 = "8";

    if (access(subPlimitsPath_0.c_str(), F_OK) == -1) {
        ret = mkdir(subPlimitsPath_0.c_str(), S_IFDIR | mode);
        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    }   
    if (access(subPlimitsPath_1.c_str(), F_OK) == -1) {
        ret = mkdir(subPlimitsPath_1.c_str(), S_IFDIR | mode);
        ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT_0);
    }
    if (access(subPlimitsPath_2.c_str(), F_OK) == -1) {
        ret = mkdir(subPlimitsPath_2.c_str(), S_IFDIR | mode);
        ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT_1);
    }
    ret = WriteFile(g_configFileMqCount_1.c_str(), g_mqLimitCount_1.c_str());
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT_1);
    ret = WriteFile(g_configFileMqCount_2.c_str(), g_mqLimitCount_2.c_str());
    ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT_1);

    Fork_017_1();
    // wpid = wait(&status);
    Fork_017_2();
    // wpid = wait(&status);

    sleep(10);
    ret = ReadFile("/proc/plimits/test0/ipc.stat", buf);
    GetLine(buf, 4, 254, array);
    mqSuccessCount = atoi(array[0] + strlen("mq success count: "));
    printf("mqSuccessCount %d\n", mqSuccessCount);
    ICUNIT_GOTO_EQUAL(mqSuccessCount, 14, mqSuccessCount, EXIT_3);
    wpid = wait(&status);
    goto EXIT_3;

EXIT_3:
    for (int k = 0; k < g_index_017_1; ++k) {
        ret = mq_close(g_mqueue_017_1[k]);
        snprintf(g_mqname_017_1, MQUEUE_STANDARD_NAME_LENGTH, "/mq_017_1_%d_%d", k, LosCurTaskIDGet());
        ret = mq_unlink(g_mqname_017_1);
    }
    for (int k = 0; k < g_index_017_2; ++k) {
        ret = mq_close(g_mqueue_017_2[k]);
        snprintf(g_mqname_017_2, MQUEUE_STANDARD_NAME_LENGTH, "/mq_017_2_%d_%d", k, LosCurTaskIDGet());
        ret = mq_unlink(g_mqname_017_2);
    }
    goto EXIT_2;
EXIT_2:
    RmdirControlFile(subPlimitsPath_2);
    ret = rmdir(subPlimitsPath_2.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    goto EXIT_1;

EXIT_1:
    RmdirControlFile(subPlimitsPath_1);
    ret = rmdir(subPlimitsPath_1.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    goto EXIT_0;

EXIT_0:
    RmdirControlFile(subPlimitsPath_0);
    ret = rmdir(subPlimitsPath_0.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    return 0;
}

void ItProcessPlimitsIpc017(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_IPC_017", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}